package com.pt.manage.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.pt.manage.domain.studentHomework;
import com.pt.manage.mapper.studentHomeworkMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.pt.manage.mapper.HomeworkMapper;
import com.pt.manage.domain.Homework;
import com.pt.manage.service.IHomeworkService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 作业Service业务层处理
 * 
 * @author wxt
 * @date 2025-02-27
 */
@Service
public class HomeworkServiceImpl implements IHomeworkService 
{
    @Autowired
    private HomeworkMapper homeworkMapper;
    @Autowired
    private studentHomeworkMapper studentHomeworkMapper;

    /**
     * 查询作业
     * 
     * @param HomeworkID 作业主键
     * @return 作业
     */
    @Override
    public Homework selectHomeworkByHomeworkID(Long HomeworkID)
    {

        return homeworkMapper.selectHomeworkByHomeworkID(HomeworkID);
    }

    /**
     * 查询作业列表
     * 
     * @param params 作业
     * @return 作业
     */
    @Override
    public List<Homework> selectHomeworkList(Map params)
    {
        List<Homework> homeworkList = homeworkMapper.selectHomeworkList(params);

        Date currentTime = new Date();
        for (Homework homework : homeworkList) {
            if(homework.getStatus()!=Homework.SUCCESS_STATUS){
            // 判断当前时间是否晚于 Deadline
            if (currentTime.after(homework.getDeadline())) {
                // 更新 status
                homework.setStatus(Homework.WAIT_STATUS); // 假设 "Expired" 是过期状态
                homeworkMapper.updateHomework(homework); // 更新数据库
            }else {
                homework.setStatus(Homework.DEFAULT_STATUS);
                homeworkMapper.updateHomework(homework); // 更新数据库
                 }
            }
        }

        return homeworkList;
    }

    /**
     * 新增作业
     * 
     * @param homework 作业
     * @return 结果
     */
    @Transactional
    @Override
    public int insertHomework(Homework homework)
    {
        homework.setStatus(Homework.DEFAULT_STATUS);
        int re =  homeworkMapper.insertHomework(homework);
        studentHomeworkMapper.insertStudentHomeworkRecord(homework.getCourseID(),homework.getHomeworkID());

        return  re;

    }

    /**
     * 修改作业
     * 
     * @param homework 作业
     * @return 结果
     */
    @Override
    public int updateHomework(Homework homework)
    {
        return homeworkMapper.updateHomework(homework);
    }

    /**
     * 批量删除作业
     * 
     * @param HomeworkIDs 需要删除的作业主键
     * @return 结果
     */
    @Override
    public int deleteHomeworkByHomeworkIDs(Long[] HomeworkIDs)
    {
        return homeworkMapper.deleteHomeworkByHomeworkIDs(HomeworkIDs);
    }

    /**
     * 删除作业信息
     * 
     * @param HomeworkID 作业主键
     * @return 结果
     */
    @Override
    public int deleteHomeworkByHomeworkID(Long HomeworkID)
    {
        return homeworkMapper.deleteHomeworkByHomeworkID(HomeworkID);
    }
}
